/**
 * 看板控制器
 * 提供数据可视化API端点
 */
const Controller = require('egg').Controller;

class DashboardController extends Controller {
  async index() {
    const { ctx } = this;
    await ctx.render('dashboard.html', {
      stats: await ctx.service.dashboard.getSystemStats(),
      architectureUrl: '/public/crm_architecture.md'
    });
  }
  /**
   * @api {get} /api/dashboard/supplier-overview 供应商概览数据
   * @apiName GetSupplierOverview
   * @apiGroup Dashboard
   * @apiPermission admin
   *
   * @apiSuccess {Number} total 供应商总数
   * @apiSuccess {Number} highRisk 高风险供应商数量
   * @apiSuccess {Number} normal 正常供应商数量
   * @apiSuccess {Number} newAdded 本月新增供应商
   */
  async getSupplierOverview() {
    const { ctx } = this;
    const result = await ctx.service.dashboard.getSupplierOverview();
    ctx.success(result);
  }

  /**
   * @api {get} /api/dashboard/risk-suppliers 风险供应商列表
   * @apiName GetRiskSuppliers
   * @apiGroup Dashboard
   * @apiPermission admin
   *
   * @apiParam {Number} [page=1] 页码
   * @apiParam {Number} [pageSize=10] 每页条数
   *
   * @apiSuccess {Array} list 风险供应商列表
   * @apiSuccess {Number} total 总条数
   */
  async getRiskSuppliers() {
    const { ctx } = this;
    const { page = 1, pageSize = 10 } = ctx.query;
    const result = await ctx.service.dashboard.getRiskSuppliers({ page, pageSize });
    ctx.success(result);
  }

  /**
   * @api {get} /api/dashboard/contract-stats 合同状态统计
   * @apiName GetContractStats
   * @apiGroup Dashboard
   * @apiPermission admin
   *
   * @apiSuccess {Object} stats 合同状态统计数据
   */
  async getContractStats() {
    const { ctx } = this;
    const result = await ctx.service.dashboard.getContractStats();
    ctx.success(result);
  }

  /**
   * @api {get} /api/dashboard/price-alerts 价格预警信息
   * @apiName GetPriceAlerts
   * @apiGroup Dashboard
   * @apiPermission admin
   *
   * @apiParam {Number} [days=7] 最近天数
   *
   * @apiSuccess {Array} list 价格预警列表
   */
  async getPriceAlerts() {
    const { ctx } = this;
    const { days = 7 } = ctx.query;
    const result = await ctx.service.dashboard.getPriceAlerts({ days });
    ctx.success(result);
  }

  /**
   * @api {get} /api/dashboard/recent-orders 最近订单数据
   * @apiName GetRecentOrders
   * @apiGroup Dashboard
   * @apiPermission admin
   *
   * @apiParam {Number} [limit=10] 数量限制
   *
   * @apiSuccess {Array} list 最近订单列表
   */
  async getRecentOrders() {
    const { ctx } = this;
    const { limit = 10 } = ctx.query;
    const result = await ctx.service.dashboard.getRecentOrders({ limit });
    ctx.success(result);
  }
}

module.exports = DashboardController;